home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / lk_lock.rand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-04  |  2.5 KB  |  114 lines

  1. #include "util.h"
  2.  
  3. /*
  4.  *    Standardized file-locking package  (RAND exclusive open)
  5.  *
  6.  *  This version assumes the existence of the RAND exclusive open
  7.  *  in the operating system.  (DPK @ BRL, 21 Dec 82)
  8.  */
  9. #define    FEXCLUSV    04    /* OR this in to get an exclusive open */
  10.  
  11. extern int errno;               /* simulate system error problems */
  12.  
  13. #ifdef DEBUG
  14. #include "ll_log.h"
  15. extern struct ll_struct *logptr;
  16. #endif
  17.  
  18. /* */
  19.  
  20. lk_open (file, access, lockdir, lockfile, maxtime)
  21. char    *file;            /* file to be locked */
  22. int    access;            /* read-write permissions */
  23. char    *lockdir;        /* --Ignored-- */
  24. char    *lockfile;        /* --Ignored-- */
  25. int    maxtime;        /* maybe break lock after it is this old */
  26. {
  27.     register int fd;
  28.  
  29. #ifdef DEBUG
  30.     ll_log (logptr, LLOGBTR, "lk_open (%s,%d,%s,%s,%d)",
  31.         file, access, lockdir, lockfile, maxtime);
  32. #endif
  33.  
  34.     if ((fd = open (file, FEXCLUSV | access)) < 0)
  35.     {
  36. #ifdef DEBUG
  37.     ll_err (logptr, LLOGBTR, "open notok (err %d)", errno);
  38. #endif
  39.     return (NOTOK);
  40.     }
  41.     return (fd);
  42. }
  43.  
  44. lk_close (fd, file, lockdir, lockfile)
  45. int    fd;
  46. char    *file;            /* file to be locked */
  47. char    *lockdir;        /* directory to put parallel file into */
  48. char    *lockfile;        /* file to lock against */
  49. {
  50.     register int retval;
  51.  
  52. #ifdef DEBUG
  53.     ll_log (logptr, LLOGBTR, "lk_close (%d,%s,%s,%s)",
  54.         fd, file, lockdir, lockfile);
  55. #endif
  56.  
  57.     if (fd < 0)
  58.     return (OK);
  59.     retval = close (fd);
  60.     return (retval);
  61. }
  62. /* */
  63.  
  64. FILE *
  65.     lk_fopen (file, access, lockdir, lockfile, maxtime)
  66. char    *file;            /* file to be locked */
  67. char    *access;        /* read-write permissions */
  68. char    *lockdir;        /* --Ignored-- */
  69. char    *lockfile;        /* --Ignored-- */
  70. int    maxtime;        /* maybe break lock after it is this old */
  71. {
  72.     register int fd;
  73.     register FILE *fp;
  74.  
  75. #ifdef DEBUG
  76.     ll_log (logptr, LLOGBTR, "lk_fopen (%s,%s,%s,%s,%d)",
  77.         file, access, lockdir, lockfile, maxtime);
  78. #endif
  79.  
  80.     if ((fd = open(file,
  81.       FEXCLUSV | (access[0]=='r' && access[1]==0 ? 0 : 2))) < 0) {
  82. #ifdef DEBUG
  83.     ll_err (logptr, LLOGBTR, "open notok");
  84. #endif
  85.     return( NULL );
  86.     }
  87.  
  88.     if ((fp = fdopen (fd, access)) == NULL)
  89.     {
  90.     (void) close( fd );
  91.     return ((FILE *) NULL);
  92.     }
  93.     return (fp);
  94. }
  95.  
  96. lk_fclose (fp, file, lockdir, lockfile)
  97. FILE    *fp;
  98. char    *file;            /* --Ignored-- */
  99. char    *lockdir;        /* --Ignored-- */
  100. char    *lockfile;        /* --Ignored-- */
  101. {
  102. #ifdef DEBUG
  103.     ll_log (logptr, LLOGBTR, "lk_fclose (%s,%s,%s)", file,
  104.         (lockdir ? lockdir : ""), (lockfile ? lockfile : ""));
  105. #endif
  106.  
  107.     switch ((int)fp) {
  108.     case EOF:
  109.     case NULL:
  110.         return (OK);
  111.     }
  112.     return(fclose (fp));
  113. }
  114.